package com.es.jdk.proxy;

import java.lang.reflect.Proxy;

public class ProxyApp {
    public static void main(String[] args) {
        //目标对象
        StudentDaoI target = new StudentDaoImpl();
        //一定要有handler
        PersistenceInvocationHandler handler = new PersistenceInvocationHandler(target);
        //获取代理对象
        StudentDaoI proxy = (StudentDaoI) Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),handler);

        proxy.delete();
        proxy.save();
        proxy.update();

        //
        TeacherDao teacherDao = new TeacherDaoImpl();
        //
        PersistenceInvocationHandler teaHandler = new PersistenceInvocationHandler(teacherDao);
        //
        TeacherDao dao = (TeacherDao) Proxy.newProxyInstance(teacherDao.getClass().getClassLoader(), teacherDao.getClass().getInterfaces(),teaHandler);
        //
        dao.saveTeacher();

        System.out.println(dao.getClass().getName());
    }
}
